﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary1;
using System.Data;
using ClassLibrary1.DAO;

namespace Bilyteam_Website.Admin.Users
{
    public partial class UserManager : System.Web.UI.Page
    {
        private int days = 1;
        private bool statusADDUser;
        private bool statusAddRole;

        Business_User_Role bs = new Business_User_Role();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Session["addRole"] = true;
                Session["addUser"] = true;
                Session["deleteUser"] = true;
                if (Session["Username"] != null)
                {
                    bindDataBirth();
                    Session["StatusRole"] = null;
                    bindDataRole();
                    btnRollBack.Enabled = false;
                    btnDelete.Enabled = false;
                    bindDataUser();
                }
                else { Response.Redirect("~/Admin/login.aspx"); }
            }
        }

        private void bindDataBirth()
        {
            ddlMonth.Items.Clear();
            ddlYear.Items.Clear();
            for (int i = 1; i <= 12; i++)
            {
                ddlDay.Items.Clear();
                for (int j = 1; j <= 31; j++)
                {
                    ddlDay.Items.Add(j.ToString());
                }
                ddlMonth.Items.Add(i.ToString());
            }

            for (int i = 1950; i <= 1994; i++)
            {
                ddlYear.Items.Add(i.ToString());
            }
        }

        //Role Manager
        private void bindDataRole()
        {

            DataTable listRole;
            ddlRole.Items.Clear();
            if (Session["Role"] != "1")
            {
                listRole = bs.getRoleWithoutAdmin();
                btnRollBack.Enabled = true;
            }
            else
            {
                listRole = bs.getRoleAll();
                btnRollBack.Enabled = false;
            }
            ddlRole.DataSource = listRole;
            ddlRole.DataTextField = "RoleName";
            ddlRole.DataValueField = "RoleID";
            ddlRole.DataBind();
            gvRole.DataSource = listRole;

            gvRole.DataBind();
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            int month = Convert.ToInt32(ddlMonth.SelectedItem.Text);
            int day = Convert.ToInt32(ddlDay.SelectedItem.Text);
            days = day;

            if (month != 8 && month % 2 == 0 && month != 2)
            {
                ddlDay.Items.Clear();
                for (int j = 1; j <= 30; j++)
                {
                    ddlDay.Items.Add(j.ToString());
                }

                if (days <= 30)
                {
                    ddlDay.Items.FindByText(days.ToString()).Selected = true;
                }
                else
                {
                    ddlDay.Items.FindByText("1").Selected = true;
                }

            }
            else if (month == 2)
            {
                ddlDay.Items.Clear();
                for (int j = 1; j <= 29; j++)
                {
                    ddlDay.Items.Add(j.ToString());
                }

                if (days <= 29)
                {
                    ddlDay.Items.FindByText(days.ToString()).Selected = true;
                }
                else
                {
                    ddlDay.Items.FindByText("1").Selected = true;
                }
            }
            else
            {
                ddlDay.Items.Clear();
                for (int j = 1; j <= 31; j++)
                {
                    ddlDay.Items.Add(j.ToString());
                }
                ddlDay.Items.FindByText(days.ToString()).Selected = true;
            }
        }

        protected void btnSubmitRole_Click(object sender, EventArgs e)
        {
            if (Session["addRole"].Equals(false))
            {
                GridViewRow row = gvRole.SelectedRow;
                int id = int.Parse(row.Cells[0].Text);
                if (bool.Parse(Session["StatusRole"].ToString()) == false)
                {
                    bs.editRole(id, txtRoleName.Text);
                    bindDataRole();
                    txtRoleName.Text = null;
                }
                else
                {


                    bs.editRole(id, txtRoleName.Text);
                    txtRoleName.Text = null;
                    Session["StatusRole"] = false;
                    bindDataRole();
                }
                Session["addRole"] = true;
            }
            else
            {
                bs.addRole(txtRoleName.Text);
                bindDataRole();
                txtRoleName.Text = null;
            }
        }

        protected void gvRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = gvRole.SelectedRow;
            int id = int.Parse(row.Cells[0].Text);
            string name = row.Cells[1].Text;
            Session["addRole"] = false;
            if (id == 1 || name == "admin")
            {
                txtRoleName.Enabled = false;
                btnSubmitRole.Enabled = false;
                btnDelete.Enabled = false;
                Session["StatusRole"] = false;
            }
            else
            {
                txtRoleName.Enabled = true;
                btnSubmitRole.Enabled = true;
                if (row.Cells[2].Text.Equals("True"))
                {
                    btnDelete.Enabled = true;
                    btnRollBack.Enabled = false;
                }
                else
                {
                    btnDelete.Enabled = false;
                    btnRollBack.Enabled = true;
                }
                txtRoleName.Text = row.Cells[1].Text;
                Session["StatusRole"] = true;
            }

        }

        protected void btnRollBack_Click(object sender, EventArgs e)
        {
            GridViewRow row = gvRole.SelectedRow;
            int id = int.Parse(row.Cells[0].Text);
            string name = row.Cells[1].Text;
            bs.rollBackRole(id);
            bindDataRole();
            btnDelete.Enabled = false;
            btnRollBack.Enabled = false;
            txtRoleName.Text = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            GridViewRow row = gvRole.SelectedRow;
            int id = int.Parse(row.Cells[0].Text);
            string name = row.Cells[1].Text;
            if (id == 1 || name == "admin")
            {
                txtRoleName.Text = "Admin không được quyền xoá";
            }
            else
            {
                bs.deleteRole(id);
                bindDataRole();
                btnDelete.Enabled = false;
                btnRollBack.Enabled = true;
            }
        }

        // User Manager

        private void bindDataUser()
        {
            DataTable listUser;
            if (Session["Role"] != "1")
            {
                listUser = bs.getUserWithoutAdmin();
            }
            else
            {
                listUser = bs.getUserAll();
            }
            gvUser.DataSource = listUser;
            gvUser.DataBind();
        }

        protected void gvUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = gvUser.SelectedRow;
            ViewState["id"] = int.Parse(row.Cells[0].Text);
            string name = row.Cells[1].Text;
            if (ViewState["id"] == (object) 1 || name == "admin")
            {
                btnDeleteUser.Enabled = false;
                txtUser.Enabled = false;
                txtUser.Text = row.Cells[1].Text;
            }
            else
            {
                btnDeleteUser.Enabled = true;
                txtUser.Enabled = false;
                txtUser.Text = row.Cells[1].Text;
                if (row.Cells[10].Text == "True")
                {
                    Session["deleteUser"] = true;
                    btnDeleteUser.Text = "Delete";
                }
                else
                {
                    Session["deleteUser"] = false;
                    btnDeleteUser.Text = "Roll Back";
                }
            }
            txtLastName.Text = row.Cells[2].Text;
            txtFirstName.Text = row.Cells[3].Text;
            txtIdentity.Text = row.Cells[4].Text;
            txtAddress.Text = row.Cells[6].Text;
            txtPhone.Text = row.Cells[7].Text;
            txtEmail.Text = row.Cells[8].Text;
            bindDataRole();
            ddlRole.Items.FindByText(row.Cells[9].Text).Selected = true;

            bindDataBirth();
            DateTime birthDay = DateTime.Parse(row.Cells[5].Text);
            ddlDay.Items.FindByText(birthDay.Day.ToString()).Selected = true;
            ddlMonth.Items.FindByText(birthDay.Month.ToString()).Selected = true;
            ddlYear.Items.FindByText(birthDay.Year.ToString()).Selected = true;

            

            txtPassword.Visible = false;
            RegularExpressionValidator2.Visible = false;
            Session["addUser"] = false;
        }

        protected void btnSubmitUser_Click(object sender, EventArgs e)
        {
            string Username = txtUser.Text;
            string Email = txtEmail.Text;
            string Identity = txtIdentity.Text;
            string Firstname = txtFirstName.Text;
            string Lastname = txtLastName.Text;
            string Phone = txtPhone.Text;
            string Address = txtAddress.Text;
            string Password = txtPassword.Text;
            int IdRole = int.Parse(ddlRole.SelectedItem.Value);
            DateTime birthDay = new DateTime();
            int day = int.Parse(ddlDay.SelectedItem.Value);
            int month = int.Parse(ddlMonth.SelectedItem.Value);
            int year = int.Parse(ddlYear.SelectedItem.Value);
            string date = month + "/" + day + "/" + year;

            birthDay = DateTime.Parse(date);

            if (Session["addUser"].Equals(true))
            {
                
                UserDAO userDao = new UserDAO(Username,Password,Firstname,Lastname,Identity,birthDay,Address,Phone,Email,IdRole);
                bs.addUser(userDao);
                bindDataUser();
            }
            else
            {
                UserDAO userDao = new UserDAO((int) ViewState["id"], Firstname, Lastname, Identity, birthDay, Address, Phone, Email, IdRole);
                bs.editUser(userDao);
                txtPassword.Visible = true;
                RegularExpressionValidator2.Visible = true;
                Session["addUser"] = true;
                ViewState["id"] = 0;
                bindDataUser();
            }
            Response.Redirect("UserManager.aspx");
        }

        protected void btnResetUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserManager.aspx");
        }

        protected void btnDeleteUser_Click(object sender, EventArgs e)
        {
            if (Session["deleteUser"].Equals(true))
            {
                bs.deleteUser((int) ViewState["id"]);
                Session["deleteUser"] = false;
                btnDeleteUser.Text = "Roll Back";
                bindDataUser();
            }
            else
            {
                bs.rollBackUser((int) ViewState["id"]);
                Session["deleteUser"] = true;
                btnDeleteUser.Text = "Delete";
                btnDeleteUser.Enabled = false;
                bindDataUser();
            }
            Response.Redirect("UserManager.aspx");
            
        }



    }
}